package com.example.myactspringboot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;

import java.text.SimpleDateFormat;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
 * @Author Zhouchb
 * @Create 2025-03-19 21:39
 * @Description
 */
@EnableAsync
@Configuration
public class MyThreadConfig {
    @Bean("threadPoolExecutor")
    public ThreadPoolExecutor threadPoolExecutor() {
        int maxPoolSize = Math.max(4, Runtime.getRuntime().availableProcessors() - 1);
        return  new ThreadPoolExecutor(4,
                maxPoolSize ,
                300,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }

    @Bean
    public ObjectMapper objectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        //在反序列化（即将 JSON 转换为 Java 对象）时忽略 JSON 中存在但 Java 类中不存在对应属性的字段。如果不设置此选项，当遇到未知属性时会抛出异常。
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        //禁止将日期序列化为时间戳格式（即长整型表示的时间），而是采用更易读的文本格式。
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
       // 设置日期格式为 "yyyy-MM-dd HH:mm:ss"
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        return objectMapper;
    }
}
